package com.sevlet;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.google.gson.GsonBuilder;
import com.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Random;
import java.util.Set;

@WebServlet("/GetValidateCodeServlet")
public class GetValidateCodeServlet extends  BaseSevlet {


    public void getcode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mobildPhone = req.getParameter("mobildPhone");


        String code = creatCode(5);//5位验证码

        HashMap<String, Object> result = null;
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();

        restAPI.init("app.cloopen.com", "8883");
        // 初始化服务器地址和端口，生产环境配置成app.cloopen.com，端口是8883.
        restAPI.setAccount("8aaf07087f77bf96017fa799349b11fa", "2acef353e49049548e388d63bc57eee6");
        // 初始化主账号名称和主账号令牌，登陆云通讯网站后，可在控制首页中看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN。
        restAPI.setAppId("8aaf07087f77bf96017fa79935ac1201");
        // 请使用管理控制台中已创建应用的APPID。
       // result = restAPI.sendTemplateSMS("号码1,号码2等","模板Id" ,new String[]{"模板内容1","模板内容2"});
        //调用以下方法就发送短信了
        result = restAPI.sendTemplateSMS(mobildPhone,"1" ,new String[]{String.valueOf(code),"5分钟"});

        JsonUtil jsonUtil = new JsonUtil();

        System.out.println("短信的发送结果 result=" + result);
        if("000000".equals(result.get("statusCode"))){//表示短信发送成功，用户手机号可以收到短信
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
            jsonUtil.setCode(0);
            jsonUtil.setMsg("短信发送成功");
            jsonUtil.setData(code);

        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            jsonUtil.setCode(1);
            jsonUtil.setMsg("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }


        PrintWriter out = resp.getWriter();
        out.print(new GsonBuilder().create().toJson(jsonUtil));




    }

    /*
     * 1、定义一个方法返回一个随机验证码
     * */
    public static String creatCode(int n) {
        //3、定义一个字符串变量记录生成的随机字符
        String code = "";
        Random r = new Random();
        //2、定义一个for循环，循环n次，依次生成随机字符
        for (int i = 0; i < n; i++) {
            //i=0 1 2
            //3、生成一个随机字符，英文大、小写 数字（0 1 2 ）
            int type = r.nextInt(3);//0 1 2
            switch (type) {
                case 0:
                    //大写字符（A 65-Z 65+25）
                    char ch = (char) (r.nextInt(26) + 65);
                    code += ch;
                    break;
                case 1:
                    //小写字符（a 97-z 97+25）
                    char ch1 = (char) (r.nextInt(26) + 97);
                    code += ch1;
                    break;
                case 2:
                    //数字字符
                    code += r.nextInt(10);//0-9
                    break;
            }
        }
        return code;
    }


}
